namespace JQuery.WebKit
{
    using System;
    using System.Collections;
    using System.Text;
    using System.Web.UI.WebControls;

    internal class JsonColModel
    {
        private WebGrid _grid;
        private Hashtable _jsonValues;

        public JsonColModel(WebGrid grid)
        {
            this._jsonValues = new Hashtable();
            this._grid = grid;
        }

        public JsonColModel(WebGridColumn column, WebGrid grid) : this(grid)
        {
            this.FromColumn(column);
        }

        private void ApplyFormatterOptions(WebGridColumn column)
        {
            if ((column.Formatter.Count > 0) && (column.Formatter[0] != null))
            {
                WebGridColumnFormatter formatter = column.Formatter[0];
                Hashtable hashtable = new Hashtable();
                if (formatter is LinkFormatter)
                {
                    LinkFormatter formatter2 = (LinkFormatter) formatter;
                    this._jsonValues["formatter"] = "link";
                    if (!string.IsNullOrEmpty(formatter2.Target))
                    {
                        hashtable["target"] = formatter2.Target;
                    }
                }
                if (formatter is EmailFormatter)
                {
                    this._jsonValues["formatter"] = "email";
                }
                if (formatter is IntegerFormatter)
                {
                    IntegerFormatter formatter3 = (IntegerFormatter) formatter;
                    this._jsonValues["formatter"] = "integer";
                    if (!string.IsNullOrEmpty(formatter3.ThousandsSeparator))
                    {
                        hashtable["thousandsSeparator"] = formatter3.ThousandsSeparator;
                    }
                    if (!string.IsNullOrEmpty(formatter3.DefaultValue))
                    {
                        hashtable["defaultValue"] = formatter3.DefaultValue;
                    }
                }
                if (formatter is NumberFormatter)
                {
                    NumberFormatter formatter4 = (NumberFormatter) formatter;
                    this._jsonValues["formatter"] = "integer";
                    if (!string.IsNullOrEmpty(formatter4.ThousandsSeparator))
                    {
                        hashtable["thousandsSeparator"] = formatter4.ThousandsSeparator;
                    }
                    if (!string.IsNullOrEmpty(formatter4.DefaultValue))
                    {
                        hashtable["defaultValue"] = formatter4.DefaultValue;
                    }
                    if (!string.IsNullOrEmpty(formatter4.DecimalSeparator))
                    {
                        hashtable["decimalSeparator"] = formatter4.DecimalSeparator;
                    }
                    if (formatter4.DecimalPlaces != -1)
                    {
                        hashtable["decimalPlaces"] = formatter4.DecimalPlaces;
                    }
                }
                if (formatter is CurrencyFormatter)
                {
                    CurrencyFormatter formatter5 = (CurrencyFormatter) formatter;
                    this._jsonValues["formatter"] = "currency";
                    if (!string.IsNullOrEmpty(formatter5.ThousandsSeparator))
                    {
                        hashtable["thousandsSeparator"] = formatter5.ThousandsSeparator;
                    }
                    if (!string.IsNullOrEmpty(formatter5.DefaultValue))
                    {
                        hashtable["defaultValue"] = formatter5.DefaultValue;
                    }
                    if (!string.IsNullOrEmpty(formatter5.DecimalSeparator))
                    {
                        hashtable["decimalSeparator"] = formatter5.DecimalSeparator;
                    }
                    if (formatter5.DecimalPlaces != -1)
                    {
                        hashtable["decimalPlaces"] = formatter5.DecimalPlaces;
                    }
                    if (!string.IsNullOrEmpty(formatter5.Prefix))
                    {
                        hashtable["prefix"] = formatter5.Prefix;
                    }
                    if (!string.IsNullOrEmpty(formatter5.Prefix))
                    {
                        hashtable["suffix"] = formatter5.Suffix;
                    }
                }
                if (formatter is CheckBoxFormatter)
                {
                    CheckBoxFormatter formatter6 = (CheckBoxFormatter) formatter;
                    this._jsonValues["formatter"] = "checkbox";
                    if (formatter6.Enabled)
                    {
                        hashtable["disabled"] = false;
                    }
                }
                if (formatter is CustomFormatter)
                {
                    CustomFormatter formatter7 = (CustomFormatter) formatter;
                    if (!string.IsNullOrEmpty(formatter7.FormatFunction))
                    {
                        this._jsonValues["formatter"] = formatter7.FormatFunction;
                    }
                    if (!string.IsNullOrEmpty(formatter7.UnFormatFunction))
                    {
                        this._jsonValues["unformat"] = formatter7.UnFormatFunction;
                    }
                }
                if (hashtable.Count > 0)
                {
                    this._jsonValues["formatoptions"] = hashtable;
                }
            }
        }

        public void FromColumn(WebGridColumn column)
        {
            string dataField = "";
            if (!string.IsNullOrEmpty(column.DataField))
            {
                dataField = column.DataField;
            }
            else if (!string.IsNullOrEmpty(column.HeaderText))
            {
                dataField = column.HeaderText;
            }
            else
            {
                dataField = this._grid.Columns.IndexOf(column).ToString() + "_template";
            }
            this._jsonValues["index"] = this._jsonValues["name"] = dataField;
            if (column.Width != 150)
            {
                this._jsonValues["width"] = column.Width;
            }
            if (!column.Sortable)
            {
                this._jsonValues["sortable"] = false;
            }
            if (column.PrimaryKey)
            {
                this._jsonValues["key"] = true;
            }
            if (!column.Visible)
            {
                this._jsonValues["hidden"] = true;
            }
            if (!column.Searchable)
            {
                this._jsonValues["search"] = false;
            }
            if (column.TextAlign != JQuery.WebKit.TextAlign.Left)
            {
                this._jsonValues["align"] = column.TextAlign.ToString().ToLower();
            }
            if (!column.Resizable)
            {
                this._jsonValues["resizable"] = false;
            }
            if (!string.IsNullOrEmpty(column.CssClass))
            {
                this._jsonValues["classes"] = column.CssClass;
            }
            switch (column.GroupSummaryType)
            {
                case GroupSummaryType.Min:
                    this._jsonValues["summaryType"] = "min";
                    break;

                case GroupSummaryType.Max:
                    this._jsonValues["summaryType"] = "max";
                    break;

                case GroupSummaryType.Sum:
                    this._jsonValues["summaryType"] = "sum";
                    break;

                case GroupSummaryType.Avg:
                    this._jsonValues["summaryType"] = "avg";
                    break;

                case GroupSummaryType.Count:
                    this._jsonValues["summaryType"] = "count";
                    break;
            }
            if (!string.IsNullOrEmpty(column.GroupTemplate))
            {
                this._jsonValues["summaryTpl"] = column.GroupTemplate;
            }
            if (column.Formatter != null)
            {
                this.ApplyFormatterOptions(column);
            }
            if (column.EditActionIconsColumn)
            {
                this._jsonValues["formatter"] = "actions";
            }
            if (column.Searchable)
            {
                if (column.SearchType != SearchType.TextBox)
                {
                    this._jsonValues["stype"] = "select";
                }
                Hashtable hashtable = new Hashtable();
                if (!string.IsNullOrEmpty(column.SearchValues))
                {
                    hashtable["value"] = column.SearchValues;
                }
                if (column.Searchable && column.SearchHidden)
                {
                    hashtable["searchhidden"] = true;
                }
                if (hashtable.Count > 0)
                {
                    this._jsonValues["searchoptions"] = hashtable;
                }
                if ((column.SearchType == SearchType.DropDown) && !string.IsNullOrEmpty(column.SearchControlID))
                {
                    Hashtable hashtable2 = new Hashtable();
                    DropDownList list = this._grid.FindControlRecursive(this._grid.Page, column.SearchControlID) as DropDownList;
                    if (list == null)
                    {
                        throw new Exception("Cannot find a DropDownList control with ID = " + column.SearchControlID);
                    }
                    if (!string.IsNullOrEmpty(list.DataSourceID) || (list.DataSource != null))
                    {
                        list.DataBind();
                    }
                    StringBuilder builder = new StringBuilder();
                    foreach (ListItem item in list.Items)
                    {
                        builder.AppendFormat("{0}:{1}", item.Value, item.Text);
                        if (list.Items.IndexOf(item) < (list.Items.Count - 1))
                        {
                            builder.Append(";");
                        }
                    }
                    list.Visible = false;
                    hashtable2["value"] = builder.ToString();
                    this._jsonValues["searchoptions"] = hashtable2;
                }
            }
            Hashtable hashtable5 = new Hashtable();
            if (!column.Visible && column.Editable)
            {
                hashtable5["edithidden"] = true;
            }

            if (column.Editable)
            {
                Hashtable hashtable3 = new Hashtable();
                this._jsonValues["editable"] = true;
                if (column.EditType != EditType.TextBox)
                {
                    this._jsonValues["edittype"] = this.GetEditType(column.EditType);
                }
                foreach (WebGridEditFieldAttribute attribute in column.EditFieldAttributes)
                {
                    hashtable3[attribute.Name] = attribute.Value;
                }
                if (!string.IsNullOrEmpty(column.EditValues))
                {
                    hashtable3["value"] = column.EditValues;
                }
                if ((column.EditType == EditType.DropDown) && !string.IsNullOrEmpty(column.EditorControlID))
                {
                    DropDownList list2 = this._grid.FindControlRecursive(this._grid.Page, column.EditorControlID) as DropDownList;
                    if (list2 == null)
                    {
                        throw new Exception("Cannot find a DropDownList control with ID = " + column.EditorControlID);
                    }
                    if (!string.IsNullOrEmpty(list2.DataSourceID) || (list2.DataSource != null))
                    {
                        list2.DataBind();
                    }
                    StringBuilder builder2 = new StringBuilder();
                    foreach (ListItem item2 in list2.Items)
                    {
                        builder2.AppendFormat("{0}:{1}", item2.Value, item2.Text);
                        if (list2.Items.IndexOf(item2) < (list2.Items.Count - 1))
                        {
                            builder2.Append(";");
                        }
                    }
                    list2.Visible = false;
                    hashtable3["value"] = builder2.ToString();
                }
                if (hashtable3.Count > 0)
                {
                    this._jsonValues["editoptions"] = hashtable3;
                }
                Hashtable hashtable4 = new Hashtable();
                if (column.EditDialogColumnPosition != 0)
                {
                    hashtable4["colpos"] = column.EditDialogColumnPosition;
                }
                if (column.EditDialogRowPosition != 0)
                {
                    hashtable4["rowpos"] = column.EditDialogRowPosition;
                }
                if (!string.IsNullOrEmpty(column.EditDialogLabel))
                {
                    hashtable4["label"] = column.EditDialogLabel;
                }
                if (!string.IsNullOrEmpty(column.EditDialogFieldPrefix))
                {
                    hashtable4["elmprefix"] = column.EditDialogFieldPrefix;
                }
                if (!string.IsNullOrEmpty(column.EditDialogFieldSuffix))
                {
                    hashtable4["elmsuffix"] = column.EditDialogFieldSuffix;
                }
                if (hashtable4.Count > 0)
                {
                    this._jsonValues["formoptions"] = hashtable4;
                }
                if (column.EditClientSideValidators != null)
                {
                    foreach (WebGridEditClientSideValidator validator in column.EditClientSideValidators)
                    {
                        if (validator is DateValidator)
                        {
                            hashtable5["date"] = true;
                        }
                        if (validator is EmailValidator)
                        {
                            hashtable5["email"] = true;
                        }
                        if (validator is IntegerValidator)
                        {
                            hashtable5["integer"] = true;
                        }
                        if (validator is MaxValueValidator)
                        {
                            hashtable5["maxValue"] = ((MaxValueValidator) validator).MaxValue;
                        }
                        if (validator is MinValueValidator)
                        {
                            hashtable5["minValue"] = ((MinValueValidator) validator).MinValue;
                        }
                        if (validator is NumberValidator)
                        {
                            hashtable5["number"] = true;
                        }
                        if (validator is RequiredValidator)
                        {
                            hashtable5["required"] = true;
                        }
                        if (validator is TimeValidator)
                        {
                            hashtable5["time"] = true;
                        }
                        if (validator is UrlValidator)
                        {
                            hashtable5["url"] = true;
                        }
                        if (validator is JQuery.WebKit.CustomValidator)
                        {
                            hashtable5["custom"] = true;
                            hashtable5["custom_func"] = ((JQuery.WebKit.CustomValidator) validator).ValidationFunction;
                        }
                        if (column.Searchable)
                        {
                            hashtable5["searchhidden"] = column.SearchHidden;
                        }

                    }
                }
            }
            else
            {
                if (column.Searchable && column.SearchHidden)
                {
                    hashtable5["searchhidden"] = true;
                }
            }
            if (hashtable5.Count > 0)
            {
                this._jsonValues["editrules"] = hashtable5;
            }
        }

        private string GetEditType(EditType type)
        {
            switch (type)
            {
                case EditType.TextBox:
                    return "text";

                case EditType.CheckBox:
                    return "checkbox";

                case EditType.DropDown:
                    return "select";

                case EditType.TextArea:
                    return "textarea";

                case EditType.Password:
                    return "password";
            }
            return "text";
        }

        public static string RemoveQuotesForJavaScriptMethods(string input, WebGrid grid)
        {
            string str = input;
            foreach (WebGridColumn column in grid.Columns)
            {
                if (((column.Formatter != null) && (column.Formatter.Count > 0)) && (column.Formatter[0] != null))
                {
                    WebGridColumnFormatter formatter = column.Formatter[0];
                    if (formatter is CustomFormatter)
                    {
                        CustomFormatter formatter2 = (CustomFormatter) formatter;
                        string oldValue = string.Format("\"formatter\":\"{0}\"", formatter2.FormatFunction);
                        string newValue = string.Format("\"formatter\":{0}", formatter2.FormatFunction);
                        str = str.Replace(oldValue, newValue);
                        oldValue = string.Format("\"unformat\":\"{0}\"", formatter2.UnFormatFunction);
                        newValue = string.Format("\"unformat\":{0}", formatter2.UnFormatFunction);
                        str = str.Replace(oldValue, newValue);
                    }
                }
                foreach (WebGridEditClientSideValidator validator in column.EditClientSideValidators)
                {
                    if (validator is JQuery.WebKit.CustomValidator)
                    {
                        JQuery.WebKit.CustomValidator validator2 = (JQuery.WebKit.CustomValidator) validator;
                        string str4 = string.Format("\"custom_func\":\"{0}\"", validator2.ValidationFunction);
                        string str5 = string.Format("\"custom_func\":{0}", validator2.ValidationFunction);
                        str = str.Replace(str4, str5);
                    }
                }
            }
            return str;
        }

        public Hashtable JsonValues
        {
            get
            {
                return this._jsonValues;
            }
            set
            {
                this._jsonValues = value;
            }
        }
    }
}

